<?php
session_start(); 

class Search extends Controller 
{

	function Intro()
		{
			parent::Controller();	
		}//end method
	
	function index()
		{
			$this->loadSettings();
			$datas	= $this->common->authenticate();
			global $userId,$now,$rootPath,$recPerPage;				//declare $userId and $now as global variable
			$data		= array();
			if(count($datas)<1)
				{
					$datas	= $this->common->basicvars();
					$data	= $datas;
					$data['siteUrl']		= site_url();
					$data['center_login']	= true;
					$this->smartyextended->view('user/login',$data);
				}
			else
				{
					$data		= $datas;
					if($_GET['submit_search'] or $this->uri->segment(2)!='')
						{
							if( $this->uri->segment(2)!='')
								$searchText	= $this->uri->segment(2);
							else
								$searchText	= $_GET['search_for'];
								
							$memberGender	= $_GET['gender'];
								
							//search for users
							$totUsers			= getTotRec("user_id","users","firstname LIKE '%$searchText%' or lastname LIKE '%$searchText%'");
							$totUsers2			= getTotRec("sex_id","general_profile","sex_id = $memberGender");
							if($totUsers>0 && $searchText!='')
								{
									$query		= "SELECT user_id FROM users WHERE firstname LIKE '%$searchText%' or lastname LIKE '%$searchText%'";
									$res		= mysql_query($query);
									$userList	= $this->common->listUsers($res,$userId);
									$content 	.= '<a href="">Результати пошуку ('.$totUsers.')</a>';
								}
								
							if($totUsers2>0 && $memberGender!='')
								{
									$query2		= "SELECT user_id FROM general_profile WHERE sex_id='".$memberGender."'";
									$res2		= mysql_query($query2);
									$userList	= $this->common->listUsers($res2,$userId);
									$content 	.= '<a href="">Результати пошуку ('.$totUsers2.')</a>';
								}
								
							
							if($content!='')
								$data['content']	= '
													<table class="tabs" cellpadding="0" cellspacing="0">
													<tr>
													<td class="tab0">&nbsp;</td>
															  <td class="tab1" NOWRAP>'.$content.' </td>
														<td class="tab">&nbsp;</td>
															<td class="tab3">&nbsp;</td>
													</tr>
													</table>

													'.$userList.'
';	
							else
								$data['content']	= '<div class="msg">Пошук за Вашим запитом не дав результатiв.</div>';
							$data['stext']		= $searchText;
						}
					$this->smartyextended->view('search',$data);
				}
		}


	function loadSettings()
		{
			global $userId,$now,$adminName,$adminEmail,$siteName,$siteTitle;
			$this->load->model('common');
			
			$userId		= $this->session->userdata('user_id');
			$now		= $this->config->item('now');
			
			$siteVars	= $this->common->getSiteVars();
			$adminName	= $siteVars['adminName'];
			$adminEmail	= $siteVars['adminEmail'];
			$siteName	= $siteVars['siteName'];
			$siteTitle	= $siteVars['siteTitle'];
		}
}
?>